package com.ls.cloud.auth.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.ls.cloud.auth.dao.AuthUserDao;
import com.ls.cloud.auth.entity.AuthUser;
import com.ls.cloud.auth.service.AuthUserService;
import com.ls.cloud.common.ResultData;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author: candy33
 * @Date: 2022/05/04 13:12
 * com.ls.cloud.cloudauth.service.impl
 */
@Service
public class AuthUserImpl implements AuthUserService {

    private final AuthUserDao userDao;

    public AuthUserImpl(AuthUserDao userDao) {
        this.userDao = userDao;
    }

    /**
     * 校验登录，登陆成功后返回token
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public ResultData login(String username, String password) {
        Optional<AuthUser> optional = userDao.findByUsername(username);
        if (optional.isEmpty()) {
            return ResultData.fail("登陆密码错误或当前账号不存在！");
        }
        StpUtil.login(1);
        return ResultData.success(StpUtil.getTokenInfo().tokenValue);
    }
}
